package app.server.project.main.service.db.dao.user;

import app.server.project.main.service.db.entity.user.RechargeOrderEntity;
import app.server.project.main.service.db.mapper.user.RechargeOrderMapper;
import app.server.project.main.service.service.recharge.PayStatus;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Administrator
 * @description 针对表【user_info(用户信息表)】的数据库操作Service实现
 * @createDate 2025-01-16 15:25:22
 */
@Repository
public class RechargeOrderDao extends ServiceImpl<RechargeOrderMapper, RechargeOrderEntity> {


    public List<RechargeOrderEntity> getWaitOrders(int payType) {
        return this.list(Wrappers.lambdaQuery(RechargeOrderEntity.class)
                .eq(RechargeOrderEntity::getStatus, PayStatus.Wait)
                .eq(RechargeOrderEntity::getPayType, payType)
        );
    }

    @Cacheable(key = "#orderId", cacheNames = "app.server.project.main.service.db.dao.getOneByOrderId")
    public RechargeOrderEntity getOneByOrderId(long orderId) {
        var ret = this.getOne(Wrappers.lambdaQuery(RechargeOrderEntity.class)
                .eq(RechargeOrderEntity::getId, orderId));
        if (ret == null) {
            return RechargeOrderEntity.of(orderId);
        }
        ret.setSynDb(true);
        return ret;
    }

    public void humanRechargeOrder(long orderId) {
        this.update(Wrappers.lambdaUpdate(RechargeOrderEntity.class)
                .eq(RechargeOrderEntity::getId, orderId)
                .ne(RechargeOrderEntity::getStatus, PayStatus.Finish)
                .set(RechargeOrderEntity::getStatus, PayStatus.Human)
        );
    }
}




